package com.campusbackend.controller;

import com.campusbackend.entity.Result;
import com.campusbackend.entity.vo.NotificationCreateVO;
import com.campusbackend.service.NotificationsService;
import com.campusbackend.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试控制器 - 用于调试通知功能
 * 
 * @author yezi
 * @since 2025-07-27
 */
@Slf4j
@RestController
@RequestMapping("/api/test")
public class TestController {
    
    @Autowired
    private NotificationsService notificationsService;
    
    /**
     * 测试发布带图片的通知
     */
    @PostMapping("/notification-with-images")
    public Result<Map<String, Object>> testNotificationWithImages() {
        try {
            // 创建测试通知
            NotificationCreateVO createVO = new NotificationCreateVO();
            createVO.setTeamId(2L); // 使用你的团队ID
            createVO.setTitle("测试通知：带图片");
            createVO.setContent("这是一个测试通知，包含图片URL");
            
            // 添加测试图片URL
            createVO.setImages(Arrays.asList(
                "http://localhost:8090/files/images/2025/07/27/test1.jpg",
                "http://localhost:8090/files/images/2025/07/27/test2.jpg"
            ));
            
            createVO.setIsUrgent((byte) 0);
            createVO.setRequireConfirmation((byte) 1);
            
            // 发布通知
            String result = notificationsService.publishNotification(
                createVO, 
                (byte) 1, // 教师
                4L, // 你的用户ID
                "测试用户"
            );
            
            Map<String, Object> response = new HashMap<>();
            response.put("result", result);
            response.put("images", createVO.getImages());
            response.put("imagesJson", com.alibaba.fastjson2.JSON.toJSONString(createVO.getImages()));
            
            log.info("测试发布通知结果: {}", result);
            log.info("图片列表: {}", createVO.getImages());
            log.info("图片JSON: {}", com.alibaba.fastjson2.JSON.toJSONString(createVO.getImages()));
            
            return ResultUtil.success(response);
            
        } catch (Exception e) {
            log.error("测试发布通知异常", e);
            return ResultUtil.fail("测试失败: " + e.getMessage());
        }
    }
    
    /**
     * 测试查询通知详情
     */
    @GetMapping("/notification/{notificationId}")
    public Result<Object> testGetNotification(@PathVariable Long notificationId) {
        try {
            var detail = notificationsService.getNotificationDetail(
                notificationId, 
                (byte) 1, // 教师
                4L // 你的用户ID
            );
            
            log.info("查询通知详情: {}", detail);
            if (detail != null) {
                log.info("通知图片: {}", detail.getImages());
            }
            
            return ResultUtil.success(detail);
            
        } catch (Exception e) {
            log.error("测试查询通知异常", e);
            return ResultUtil.fail("查询失败: " + e.getMessage());
        }
    }
}
